#include "BoundedBuffer.h"

BoundedBuffer::BoundedBuffer(int maxSize):Buffer()
{
	m_empty = new CountingSemaphore(maxSize);
	m_maxSize = maxSize;
}

BoundedBuffer::~BoundedBuffer(void)
{
	delete m_empty;
}

void BoundedBuffer::push(Message* m, Thread* t)
{
	m_empty->down(t);
	m_mutex->lock(t);
	m_buffer->push(m);
	m_mutex->unlock();
	m_full->up(t);
}

Message* BoundedBuffer::pop(Thread* t)
{
	//Message* m = Buffer::pop(t);
	m_full->down(t);
	m_mutex->lock(t);
	Message* m = m_buffer->front();
	m_buffer->pop();
	m_mutex->unlock();
	m_empty->up(t);

	return m;
}

int BoundedBuffer::getMaxSize()
{
	return m_maxSize;
}
